// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "colorspace_conversions_ex.h"
#include "compat.h"
#include "image.h"

namespace harmonizer {

void ConvertRGBToHSV(uint8 r, uint8 g, uint8 b,
                     double* h, double* s, double* v)
{
  // TODO
  CHECK(false);
}

void ConvertHSVToRGB(double h, double s, double v,
                     uint8* r, uint8* g, uint8* b) {
  // TODO
  CHECK(false);
}

bool ConvertRGBToHSVFloat(const Image& rgb,
                          FloatImage* hsv) {
  CHECK(hsv);
  const int channels = rgb.channels();
  if (channels != 3) {
    return false;
  }

  hsv->Resize(rgb.width(), rgb.height(), FloatImage::RGB);

  const uint8* rgb_pixels = rgb.pixel_data();
  float* hsv_pixels = hsv->mutable_pixel_data();

  const int num_pixels = rgb.width() * rgb.height();
  for (int i = 0; i < num_pixels; ++i) {
    double hue = 0;
    double saturation = 0;
    double value = 0;
    ConvertRGBToHSV(rgb_pixels[0], rgb_pixels[1],
                    rgb_pixels[2], &hue, &saturation,
                    &value);

    hsv_pixels[0] = static_cast<float>(hue);
    hsv_pixels[1] = static_cast<float>(saturation);
    hsv_pixels[2] = static_cast<float>(value);

    rgb_pixels += channels;
    hsv_pixels += channels;
  }

  return true;
}

bool ConvertHSVToRGBFloat(const FloatImage& hsv,
                          Image* rgb) {
  CHECK(rgb);
  const int channels = hsv.channels();
  if (channels != 3) {
    return false;
  }

  rgb->Resize(hsv.width(), hsv.height(), Image::RGB);

  const float* hsv_pixels = hsv.pixel_data();
  uint8* rgb_pixels = rgb->mutable_pixel_data();

  const int num_pixels = hsv.width() * hsv.height();
  for (int i = 0; i < num_pixels; ++i) {
    ConvertHSVToRGB(hsv_pixels[0], hsv_pixels[1],
                    hsv_pixels[2], &rgb_pixels[0],
                    &rgb_pixels[1], &rgb_pixels[2]);

    rgb_pixels += channels;
    hsv_pixels += channels;
  }

  return true;
}

}  // namespace harmonizer
